package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.impl.OpenUserAdd;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.UserDiscountMapper;
import com.koron.css2.serviceManage.mapper.UserMixtureMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class PjMeterBatchUpdate implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            List<PjMeter> pjMeterList = JsonUtils.objectToList(req.getData(), PjMeter.class);

            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            UserMixtureMapper userMixtureMapper = factory.getMapper(UserMixtureMapper.class);
            UserDiscountMapper userDiscountMapper = factory.getMapper(UserDiscountMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);

            for (PjMeter pjMeter : pjMeterList) {
                // 1. 更新客户
                CtmInfoBean ctmInfoBean = pjMeter.getCtmInfo();
                // 1.1 clientAddrId->clientAddr
                if (StringUtils.isNotEmpty(ctmInfoBean.getClientAddrId())) {
                    ctmInfoBean.setClientAddr(ctmInfoBean.getClientAddrId());
                }
                // 1.2 updateTime, updateName, updateAccount
                ctmInfoBean.setUpdateInfo(userInfo);
                ctmInfoMapper.ctmInfoUpdate(ctmInfoBean, CtmInfoBean.CTMTEMP);
                // 2. 更新用户
                UserBean userBean = pjMeter.getUserInfo();
                // 2.1 ctmName,certNo,certType,likeTel,mobile,faxNumber,ctmAddr
                OpenUserAdd openUserAdd = new OpenUserAdd();
                openUserAdd.ctmInfoToUserInfo(ctmInfoBean, userBean);
                // 2.2 clientContractAddrId->clientContractAddr
                if (StringUtils.isNotEmpty(userBean.getClientContractAddrId())) {
                    userBean.setClientContractAddr(userBean.getClientContractAddrId());
                }
                // 2.3 updateTime, updateName, updateAccount
                userBean.setUpdateInfo(userInfo);
                userInfoMapper.userInfoUpdate(userBean, UserBean.USERTEMP);
                // 3. 更新混合用水
                PjSave pjSave = new PjSave();
                pjSave.saveMeterUserMixture(userMixtureMapper, userInfo, pjMeter, userBean.getUserNo());
                // 4. 更新费用折扣
                pjSave.saveMeterUserDiscount(userDiscountMapper, userInfo, pjMeter, userBean.getUserNo());
                // 5. 更新账户
                AccountInfoBean accountInfoBean = pjMeter.getAccountInfo();
                // 5.1 updateTime, updateName, updateAccount
                accountInfoBean.setUpdateInfo(userInfo);
                accountInfoMapper.accountInfoUpdate(accountInfoBean, AccountInfoBean.ACCOUNTTEMP);
            }
            MessageBean<String> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "立户批量数据更新成功", String.class);
            messageBean.setData("立户批量数据更新成功");
            return messageBean;
        } catch (Exception e) {
            logger.error("立户批量数据更新失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "立户批量数据更新失败", null);
        }
    }
}
